﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class ArticlePage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        Response.Write(id);
        string sql = "SELECT * FROM Articles WHERE ArticleID=" + id;

        if (Session["isLogIn"].Equals("true"))
        {
            Login.Visible = false;
            TxtLogin.Visible = false;
            Password.Visible = false;
            TxtPassword.Visible = false;
            Log_In.Visible = false;
            LblError.Visible = false;
            LinkRegistration.Visible = false;
            LblIsLogIn.Visible = true;
            LblIsLogIn.Text = "Jesteś zalogowany jako " + Session["login"];
            Log_Out.Visible = true;
        }
        else
        {
            Login.Visible = true;
            TxtLogin.Visible = true;
            Password.Visible = true;
            TxtPassword.Visible = true;
            Log_In.Visible = true;
            LinkRegistration.Visible = true;
            Log_Out.Visible = false;
            LblError.Visible = false;
            LblIsLogIn.Visible = false;
        }
    }

    protected void Log_In_Click(object sender, EventArgs e)
    {
        string login = TxtLogin.Text;
        string password = TxtPassword.Text;

        var sql = "SELECT COUNT(*) FROM Users WHERE login LIKE '" + login + "' AND password LIKE '" + password + "'";

        SqlDataReader readerUsers = DataBase.SelectData(sql);

        readerUsers.Read();

        if (login.Equals("admin") & password.Equals("admin"))
        {
            Session["login"] = login;
            Session["isLogIn"] = "true";
            readerUsers.Close();
            Response.Redirect("AdministrationPanel.aspx");
        }

        //moze byc kilka takich samych loginow i hasel
        if (readerUsers.GetInt32(0) >= 1)
        {
            Session["login"] = login;
            Session["isLogIn"] = "true";
            readerUsers.Close();
            Response.Redirect("UserProfile.aspx");
            Login.Visible = false;
            TxtLogin.Visible = false;
            Password.Visible = false;
            TxtPassword.Visible = false;
            Log_In.Visible = false;
            LblError.Visible = false;
            LinkRegistration.Visible = false;
            LblIsLogIn.Visible = true;
            LblIsLogIn.Text = "Jesteś zalogowany jako " + Session["login"];
            Log_Out.Visible = true;
        }
        else
        {
            readerUsers.Close();
            Login.Visible = true;
            TxtLogin.Visible = true;
            Password.Visible = true;
            TxtPassword.Visible = true;
            Log_In.Visible = true;
            LinkRegistration.Visible = true;
            Log_Out.Visible = false;
            LblError.Visible = true;
            TxtLogin.Text = "";
            TxtPassword.Text = "";
        }

        readerUsers.Close();
    }

    protected void Log_Out_Click(object sender, EventArgs e)
    {
        Session["isLogIn"] = "false";
        Response.Redirect("Default.aspx");
        Login.Visible = true;
        TxtLogin.Visible = true;
        Password.Visible = true;
        TxtPassword.Visible = true;
        Log_In.Visible = true;
        LinkRegistration.Visible = true;
        Log_Out.Visible = false;
        LblIsLogIn.Visible = false;
        LblIsLogIn.Text = "";
        TxtLogin.Text = "";
        TxtPassword.Text = "";
    }

    protected void LinkRegistration_Click(object sender, EventArgs e)
    {
        Response.Redirect("Registration.aspx");
    }

    protected void BtnSearch_Click(object sender, EventArgs e)
    {
        //string url = String.Format("~/SearchResult.aspx?SearchField={0}", TxtSearch.Text);
        //Response.Redirect(url);
        string searchWord = TxtSearch.Text;
        if (!searchWord.Equals(""))
        {
            Response.Redirect("SearchResult.aspx?SearchField=" + searchWord);
        }
        searchWord = "";
    }

    protected void LinkLogo_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
}